package com.zxd.streams;


import com.zxd.clones.Students;
import com.zxd.template.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * ClassName:ProcessStream
 * Package:com.zxd.streams
 * 我的理解是流是从左边开始处理的
 * 比如说要取出身高前三的学生
 * 需要倒序排列 198 187 173 172 165 142
 * @Date:2023/9/24 21:48
 * @Author:zxde
 */
public class ProcessStream {
    public static void main(String[] args) {
        List<Double> scores = new ArrayList<>();
        Collections.addAll(scores, 88.5, 100.0, 60.0, 99.0, 9.5, 99.6, 25.0);
        // 需求1: 找出成绩大于等于60分的数据，并升序后，再输出。
        scores.stream().filter(score -> score >= 60).sorted(Double::compare).forEach(System.out::println);
        System.out.println("------------------------------------------------------------");
        List<StreamStudent> streamStudent = new ArrayList<>();
        StreamStudent s1 = new StreamStudent("蜘蛛精", 26, 172.5);
        StreamStudent s2 = new StreamStudent("蜘蛛精", 26,172.5);
        StreamStudent s3 = new StreamStudent("紫霞", 23,167.6);
        StreamStudent s4 = new StreamStudent("白晶晶", 25,169.0);
        StreamStudent s5 = new StreamStudent("牛魔王", 35,183.3);
        StreamStudent s6 = new StreamStudent("陈淼", 34,168.5);
        Collections.addAll(streamStudent, s1, s2, s3, s4, s5, s6);
        // 需求2:找出年龄大于等于23，且年龄小于等于30 岁的学生，并按照年龄峰序输出
        streamStudent.stream().filter(student -> student.getAge() >= 23 &&   student.getAge() <= 30)
                .sorted((o1, o2) -> o2.getAge()-o1.getAge())
                .forEach(System.out::println);
        System.out.println("------------------------------------------------------------");
        // 需求3: 取出身高最高的前3名学生，并输出。
        streamStudent.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
                .limit(3)
                .forEach(System.out::println);
        System.out.println("------------------------------------------------------------");

        // 需求4: 取出身高倒数的2名学生，并升序输出使用skip
        streamStudent.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
                .skip(streamStudent.size() - 2)
                .forEach(System.out::println);
        System.out.println("------------------------------------------------------------");

        // 需求5: 找出身高超过168的学生叫什么名字，要求去除重复的名字，再输出
        streamStudent.stream().filter(student -> student.getHeight() > 168)
                .map(student -> student.getName()) //map，将student对象映射为name存储到流中
                .distinct() //去重
                .forEach(System.out::println);
        System.out.println("------------------------------------------------------------");

        Stream<String> st1 = Stream.of("1", "2");
        Stream<String> st2 = Stream.of("1", "2","3", "4");
        Stream.concat(st1,st2)
                .distinct()
                .forEach(System.out::println);

    }
}